import Reflux from 'reflux';
import {Control} from "react-keeper";
import Actions from "./Actions";
import update from 'react-addons-update';
import DB from '../../../../app/db';

import StorageUtils from "../../../../app/storage";
import Utils from "../../../../utils/index";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    voteId: '',
    title: '',
    startTime: '',
    endTime: '',
    roleIntroduce: '',
    allDistrict: [],
    myDistrict: [],
    userInfo: [],
    courtsAndDoors: []
  },

  init() {
  },

  initState(vote) {
    this.data.userInfo = update(this.data.userInfo, {$set: StorageUtils.getStorage().get('user')});
    this.updateComponent();
    // 获取投票小区信息
    this.data = update(this.data, {
      voteId: {$set: vote.voteId},
      title: {$set: vote.title},
      startTime: {$set: vote.startTime},
      endTime: {$set: vote.endTime},
      roleIntroduce: {$set: vote.roleIntroduce},
      allDistrict: {$set: vote.allDistrict},
      myDistrict: {$set: vote.myDistrict},
      // 方案详细描述
      description: {$set: vote.planIntroduce},
      // 方案是否处于投票中状态
      active: {$set: vote.active},
      // 是否已投
      isVoted: {$set: false}
    });
    this.updateComponent();
    this.getDoorGroupCourt(this.data.userInfo.proprietorId);
  },

  getDoorGroupCourt(proprietorId) {
    let t = this;
    DB.WechatAPI.getDoorGroupCourt({proprietorId: proprietorId}).then(
      function (content) {
        t.data.courtsAndDoors = update(t.data.courtsAndDoors, {$set: content});
        t.updateComponent();
        t.getDistrictByVoteId(t.data.voteId);
      },
      function (error) {
        console.info(error);
      }
    );
  },

  getDistrictByVoteId(voteId) {
    let t = this;
    let param = {planid: voteId};
    DB.WechatAPI.getDistrictByVoteId(param).then(
      function (content) {
        t.data.allDistrict = content;
        t.data.myDistrict = t.data.allDistrict.filter(district => {
          let isMy = false;
          t.data.courtsAndDoors.map(court => {
            if (court.id === district.id) {
              isMy = true;
            }
          });
          return isMy;
        });
        t.updateComponent();
        t.loadVoteState();
      },
      function (error) {
        t.data.myDistrict = [];
        t.data.allDistrict = [];
        t.updateComponent();
        Utils.myAlert(error);
      }
    ).catch(function (reason) {
      Utils.myAlert(reason);
    });
  },

  loadVoteState() {
    let t = this;
    let myDistrict = t.data.myDistrict;
    // let newDistrict = [];
    myDistrict.map(d => {
      let isVoted = false;
      let proprietor = StorageUtils.getStorageItem('proprietor');
      let params = {
        proprietorId: proprietor.id,
        planId: Control.state.voteId,
        courtId: d.id
      };
      DB.WechatAPI.getVoteResult(params).then(
        function (content) {
          let isVoted = content && content.vrList && content.vrList.length > 0;
          let newDistrict = t.data.myDistrict.map(d => {
            if (d.id === params.courtId) {
              return update(d, {isVoted: {$set: isVoted}});
            } else {
              return d;
            }
          });
          t.data.myDistrict = update(t.data.myDistrict, {$merge: newDistrict});
          t.updateComponent();
        },
        function (error) {
          Utils.myAlert(error);
        }
      );
    });
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState() {
    return this.data;
  }
});
